﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BlackJack
{
    public class Card : System.Object
    {
        #region NestTypes
        public enum CardType
        {
            Ace = 1,
            Two = 2,
            Three = 3,
            Four = 4,
            Five = 5,
            Six = 6,
            Seven = 7,
            Eight = 8,
            Nine = 9, 
            Ten = 10,
            Jack = 11,
            Queen = 12, 
            King = 13
        }
        public enum CardSuit
        {
            Clubs = 0,
            Diamonds = 1,
            Hearts = 2,
            Spades = 3
        }
        #endregion

        #region Private
        private CardType m_cardType;
        private CardSuit m_cardSuit;
        private BitmapImage m_cardImage;
        private int m_value;
        private int m_trueValue;
        private Thickness m_margin;
        #endregion

        #region Methods
        public Card(CardType type, CardSuit suit)
        {
            m_cardSuit = suit;
            m_cardType = type;
            m_value = ((int)suit * 13) + (int)m_cardType;            
            m_trueValue = (int)m_cardType;
            if (m_trueValue > 10)
                m_trueValue = 10;
        }
        public Image DrawCard()
        {
            Uri imageUri = new Uri(string.Format("Images/{0}.jpg", m_value), UriKind.Relative);
            BitmapImage bi;
            try
            {
                bi = new BitmapImage(imageUri);
            }
            catch (Exception ex)
            { throw new Exception("Could not load the Image", ex); }
            Image cardImg = new Image
                {
                    Source = bi,
                    Height = 96,
                    Width  = 71                   
                };
            //Create a rectangle to hold the image
            //Rectangle cardRectangle = new Rectangle();
            //cardRectangle.Height = 96; cardRectangle.Width = 71;
            //cardRectangle.Margin = m_margin;
            //ImageBrush imgBrush = new ImageBrush();
            //imgBrush.ImageSource = m_cardImage;
            //cardRectangle.Fill = imgBrush;
            return (cardImg);
        }

        public override bool Equals(object obj)
        {
            Card c = (Card)obj;
            if (c == null)
                throw new InvalidCastException("Can't cast object to Card datatype");
            
            return (this.FaceValue==c.FaceValue && this.CardSuitType==c.CardSuitType);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion 
        
        #region Properties
        public CardType FaceValue
        {
            get { return m_cardType; }
        }
        public CardSuit CardSuitType
        {
            get { return m_cardSuit; }
        }
        public int TrueValue
        {
            get { return m_trueValue; }
        }
        public BitmapImage CardImage
        {
            get { return m_cardImage; }
        }
        public Thickness Margin
        {
            get { return m_margin; }
            set { m_margin = value; }
        }

        #endregion
    }
}
